package com.event.usc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.event.usc.dto.*;
import com.event.usc.oracleJDBC.DatabaseHandler;
import com.google.gson.Gson;

@WebServlet("/EventServlet")
public class EventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String qtype = request.getParameter("q");
		if (qtype.equals("detail"))
		{
			long id = Long.parseLong(request.getParameter("eventid"));
			doDetailRequest(id, response);
		}
		else
		{
			int low = Integer.parseInt(request.getParameter("low"));
			int high = Integer.parseInt(request.getParameter("high"));
			long start = Long.parseLong(request.getParameter("start"));
			long end = Long.parseLong(request.getParameter("end"));
			//String cat = request.getParameter("cat").trim().toUpperCase();
			String key = request.getParameter("key").trim().toLowerCase();
			String[] cat = request.getParameterValues("cat");
			for (int i=0; i<cat.length; i++)
				cat[i] = cat[i].trim().toUpperCase();
			doAbbRequest(low, high, start, end, cat, key, response);
		}
	}

	private void doAbbRequest(int low, int high, long start, long end,
			String[] cat, String key, HttpServletResponse response) {
		if (contains(cat,"ALL")&&key.equals(""))
			doTimeRequest(low, high, start, end, response);
		else if (contains(cat,"ALL")&&!key.equals(""))
			doKeyRequest(low, high, start, end, key, response);
		else if (!contains(cat, "ALL")&&key.equals(""))
			doCatRequest(low, high, start, end, cat, response);
		else
			doFilterRequest(low, high, start, end, cat, key, response);		
	}

	private boolean contains(String[] cat, String string) {
		for (String s:cat)
		{	
			if (s.equals(string))
				return true;
		}
		return false;
	}

	private void doFilterRequest(int low, int high, long start, long end,
			String[] cat, String key, HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<EventAbb> result = handler.getFilterEventAbbs(low, high, start, end, cat, key);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void doKeyRequest(int low, int high, long start, long end,
			String key, HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<EventAbb> result = handler.getKeyEventAbbs(low, high, start, end, key);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void doDetailRequest(long id, HttpServletResponse response) {
		
		try {
			DatabaseHandler handler = new DatabaseHandler();
			EventDT result = handler.getEventDT(id);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void doCatRequest(int low, int high, long start, long end,
			String[] cat, HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<EventAbb> result = handler.getCatEventAbbs(low, high, start, end, cat);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void doTimeRequest(int low, int high, long start, long end, HttpServletResponse response) 
	{
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<EventAbb> result = handler.getTimeEventAbbs(low, high, start, end);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private <T> void printResponse(HttpServletResponse response, List<T> result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write(getOutString(result));
	}
	
	private <T> void printResponse(HttpServletResponse response, T result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write(getOutString(result));
	}

	private <T> String getOutString(List<T> result) {
		String outstring;
		if (result==null || result.size()==0)
        	outstring = "[]";
        else
        {
        	Gson gson = new Gson();
        	outstring = gson.toJson(result);
        }
		return outstring;
	}
	
	private <T> String getOutString(T result) {
		String outstring;
		if (result==null)
        	outstring = "{'id': -1}";
        else
        {
        	Gson gson = new Gson();
        	outstring = gson.toJson(result);
        }
		return outstring;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
